<script>
import axios from 'axios';

export default {
  name: 'Add',
  props: {
    dialogVisible: Boolean,
  },
  data() {
    return {
      Client: {
        Cname: '',
        Phone: '',
        Sex: '',
        Birthday: null,
        MType: 1,
        MenberNum: 0,
        Pwd:'000000'
      },
      memberTypes: [], // 用于存储会员类型数据
      rules: {
        Cname: [{ required: true, message: '请输入名字', trigger: 'blur' }],
        Phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请输入有效的手机号',
            trigger: 'blur'
          }
        ],
        Sex: [{ required: true, message: '请选择性别', trigger: 'change' }],
        Birthday: [{ required: true, message: '请选择生日', trigger: 'change' }],
        MType: [{ required: true, message: '请选择会员类型', trigger: 'change' }]
      
      }
    };
  },
  mounted() {
    // 加载会员类型数据
    this.getMemberTypes();
  },
  methods: {
    getMemberTypes() {
      axios
        .get('http://localhost:21371/Api/MemberType/All')
        .then((res) => {
          //console.log('后端返回的数据:', res.data); // 打印数据
          if (res.data && res.data.data && Array.isArray(res.data.data)) {
            this.memberTypes = res.data.data;
          } else {
            console.error('后端返回的数据格式错误:', res.data);
          }
        })
        // .catch((error) => {
        //   this.$message.error('获取会员类型失败，请稍后重试');
        // });
    },
    cancel() {
      this.$refs.storeForm.resetFields();
      this.$emit('update:dialogVisible', false);
    },
    addStore() {
      this.$refs.storeForm.validate((valid) => {
        if (valid) {
          console.log('会员信息:', this.Client);
          axios
            .post('http://localhost:21371/Api/Client/Addclient', this.Client)
            .then(() => {
              this.$message({ type: 'info', message: '密码初始化为:000000,请及时修改密码!' });
              this.$emit('update:dialogVisible', false);
              this.$emit('refresh');
            })
            .catch((error) => {
              console.error('添加会员失败:', error);
              this.$message.error('添加会员失败，请稍后重试');
            });
        }
      });
    }
  }
};
</script>

<template>
  <div>
    <el-form :model="Client" :rules="rules" ref="storeForm">
      <el-form-item label="名字" prop="Cname">
        <el-input v-model="Client.Cname" placeholder="请输入名字"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="Phone">
        <el-input v-model="Client.Phone" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="Sex">
        <el-radio-group v-model="Client.Sex">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="生日" prop="Birthday">
        <el-date-picker
          v-model="Client.Birthday"
          type="date"
          placeholder="选择生日"
          style="width: 100%;"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="会员类型" prop="MType">
        <el-select v-model="Client.MType" disabled placeholder="请选择会员类型">
          <el-option
            v-for="type in memberTypes"
            :key="type.MTId"
            :label="type.MTname"
            :value="type.MTId"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="会员积分" prop="MenberNum">
        <el-input
          v-model.number="Client.MenberNum"
          type="number"
          readonly="true"
          placeholder="请输入会员积分"
        ></el-input>
      </el-form-item>
    </el-form> -->
    <el-form-item label="会员积分" prop="MenberNum">
  <el-input
    v-model.number="Client.MenberNum"
    type="number"
    readonly="true"
    placeholder="请输入会员积分"
  ></el-input>
  <!-- 新增提示文字 -->
  <div class="form-tip" style="color: red; font-size: 12px; margin-top: 5px;">
    新添加的会员积分初始化为0
  </div>
</el-form-item>
</el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="addStore">确 定</el-button>
    </span>
  </div>
</template>

<style scoped>
.el-form-item {
  margin-bottom: 20px;
}

.el-form {
  max-width: 400px;
}
</style>
